<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1. 封装一个函数 输入女朋友数组  返回平均年龄
        var gfList = [
            {
                name: 're8',
                height: 180,
                weight: 180,
                age: 100,
            },
            {
                name: 'nazha',
                height: 180,
                weight: 180,
                age: 140,
            },
            {
                name: 'tongtong',
                height: 180,
                weight: 180,
                age: 180,
            },
        ];

        // 得到平均年龄
        function getAvgAge (arr) {
            var sum = 0;
            for (var i = 0; i < arr.length; i++) {
                sum += arr[i].age;
            }
            return sum / arr.length;
        }

        console.log(getAvgAge(gfList)); // 140

        // 2. 封装一个函数 输入对象张三  返回张三孩子的平均年龄
        var zs = {
            name: 'zhangsan',
            age: 88,
            children: [
                {
                    name: 'lisi',
                    age: 87,
                },
                {
                    name: 'wangwu',
                    age: 86,
                },
                {
                    name: 'liuliu',
                    age: 18,
                },
            ],
        };

        console.log(getAvgAge(zs.children));


        // 1. 封装一个函数 输入女朋友数组 返回女朋友名字组成的数组
        // 输入
        var gfList = [
            {
                name: 're8',
                height: 180,
                weight: 180,
                age: 100,
            },
            {
                name: 'nazha',
                height: 180,
                weight: 180,
                age: 140,
            },
            {
                name: 'tongtong',
                height: 180,
                weight: 180,
                age: 180,
            },
        ];
        // 期待输出
        var result = [
            're8',
            'nazha',
            'tongtong',
        ];

        // 80%数组处理  都是这种映射形式 arr.map()
        function getNameList (arr) {
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                result.push(arr[i].name);
            }
            return result;
        }


        // 2. 封装一个函数 输入女朋友数组 返回身高大于170的女朋友数组
        // 输入
        var gfList = [
            {
                name: 're8',
                height: 180,
                weight: 180,
                age: 100,
            },
            {
                name: 'nazha',
                height: 175,
                weight: 180,
                age: 140,
            },
            {
                name: 'tongtong',
                height: 150,
                weight: 180,
                age: 180,
            },
        ];
        // 期待输出
        var result = [
            {
                name: 're8',
                height: 180,
                weight: 180,
                age: 100,
            },
            {
                name: 'nazha',
                height: 175,
                weight: 180,
                age: 140,
            },
        ];

        function filterHeight (arr) {
            var result = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].height > 170) {
                    result.push(arr[i]);
                }
            }
            return result;
        }

    </script>
</body>
</html>